subroutine read_aliases(newnames,na)

!
! n_al indica il numero di periodi considerati (e quindi di aliases assegnati).
! se n_al= -2, allora si hanno due canali che separamo accelerometro e velocimetro
! (indipendentemente dalla data).
!
!#station,,station,,station,,station,,,
!IMOL,0,IMOLh,2005-04-16 00:00:00,IMOLn,2005-04-16 00:00:00,,,,
!MODR,2,MODRx,2005-04-16 00:00:00,MODR,2009-05-20 00:00:00,,,,
!MNS,2,MNSx,2005-04-16 00:00:00,MNS,2006-04-01 00:00:00,,,,
!PLAC,2,PLACx,2005-04-16 00:00:00,PLAC,2008-07-01 00:00:00,,,,
!SENI,3,SENIx,2005-04-16 00:00:00,SENIy,2009-05-01 00:00:00,SENI,2011-09-01 00:00:00,,
!CRMI,3,CRMI,2005-04-16 00:00:00,CRMIx,2007-05-01 00:00:00,CRMI,2012-11-01 00:00:00,,
!CAMP,4,CAMP,2005-04-16 00:00:00,CAMPx,2006-12-01 00:00:00,CAMPy,2009-04-07  00:00:00,CAMP,2009-05-06 00:00:00
!IFIL,4,IFILx,2005-04-16 00:00:00,IFILx,2007-11-01 00:00:00,IFILw,2011-06-01 00:00:00,IFIL,2012-03-01 00:00:00

    USE PARSE_CSV
    include 'ALIASES_structure.FI'
 
    character*80 fil
    character*1024 line
    logical success
    integer istart,iend,len,ich,i1,i2,add,i,ier,na
    character*1 sep
    data sep/','/

    write(*,*)' subroutine: read_aliases'
    is=0
! open ALIASES_TABLE.CSV

    fil='ALIASES_TABLE.csv'   !!!
    open(status='old',readonly,unit=2,file=fil,err=99)
    na=0
    2 read(2,'(q,a)',end=9)len,line
    

    if(line(1:1).eq.'#')then
! it is only a comment
       goto 2
    else    
!  READ the alisases of a station
      na=na+1
      i1=1

      call parse_csv_var(sep,ier,line,i1,i2,len,newnames(na).s_code,ich)
      call parse_csv_var(sep,ier,line,i1,i2,len,newnames(na).n_al)

      j=ABS(newnames(na).n_al)
      do k=1,j
        call parse_csv_var(sep,ier,line,i1,i2,len,newnames(na).al(k).al_code,ich)
        call parse_csv_var(sep,ier,line,i1,i2,len,newnames(na).al(k).start_date,ich)
      enddo      
 
    endif
    write(*,'(a)')line(1:len)
    goto 2

9   write(*,*)' I have read ',na,' aliases records'
    !pause!pause
    return

99  write(*,*)' error in reading ALIASES at record ',na
    pause 
    stop
return
end
! **************************************************

